import 'package:device_identity/device_identity.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:yqmssa_app/utils/shared_utils.dart';

class GetDeviceInfo {
  GetDeviceInfo._();

  static Future<AndroidDeviceInfo> _getDeviceInfo() async {
    // 获取设备信息
    final deviceInfo = await DeviceInfoPlugin().androidInfo;
    return deviceInfo;
  }

  //版本型号
  static Future<String> getVersion() async {
    final androidInfo = await _getDeviceInfo();
    return androidInfo.model;
  }

  //设备品牌
  static Future<String> getBrand() async {
    final androidInfo = await _getDeviceInfo();
    return androidInfo.brand;
  }

  //设备android版本
  static Future<String> getAndroidVersion() async {
    final androidInfo = await _getDeviceInfo();
    return androidInfo.version.release;
  }

  static Future<String> getOaID() async {
    if (sharedUtils.oaId.isNotEmpty) return sharedUtils.oaId;
    await DeviceIdentity.register();
    String oaID = await DeviceIdentity.oaid;
    int retryCount = 0;
    do {
      await DeviceIdentity.register();
      oaID = await DeviceIdentity.oaid;
      sharedUtils.oaId = oaID;
      retryCount++;
    } while (oaID.isEmpty || retryCount <= 10);
    return oaID;
  }

  static Future<String> getImei() async {
    await DeviceIdentity.register();
    String imei = await DeviceIdentity.imei;
    int retryCount = 0;
    do {
      await DeviceIdentity.register();
      imei = await DeviceIdentity.imei;
      retryCount++;
    } while (imei.isEmpty && retryCount <= 10);
    return imei;
  }
}
